﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Caching;

namespace Enter.Common
{
   public class CacheHelper
    {   
       /// <summary>
        /// 缓存委托
        /// </summary>
        /// <returns></returns>
        public delegate object CacheDelegate();
        /// <summary>
        /// 是否使用缓存
        /// </summary>
        public  static bool IsCache()
        {
            return true;
        }

        /// <summary>
        /// 缓存对象
        /// </summary>
        private static readonly System.Web.Caching.Cache _objCache;
        /// <summary>
        /// 缓存过期时间
        /// 单位为分钟
        /// </summary>
        private const int CACHETIMEOUT = 100;
        /// <summary>
        /// 线程锁定
        /// </summary>
        private static object privateObjectLock = new object();
        /// <summary>
        /// 静态构造函数
        /// </summary>
        static CacheHelper()
        {
            lock (privateObjectLock)
            {
                _objCache = System.Web.HttpRuntime.Cache;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="cachekey"></param>
        public static void Remove(string cachekey)
        {
            _objCache.Remove(cachekey);
        }
        /// <summary>
        /// 获取缓存
        /// </summary>
        /// <param name="cachekey"></param>
        /// <returns></returns>
        public static object Get(string cachekey)
        {
            return _objCache[cachekey];
        }
        /// <summary>
        /// 设置缓存
        /// </summary>
        /// <param name="cachekey"></param>
        /// <param name="val"></param>
        public static void Set(string cachekey, object val)
        {
            if (IsCache())
            _objCache.Insert(cachekey, val);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="cachekey"></param>
        /// <param name="val"></param>
        /// <param name="sildingExpiration"></param>
        public static void Set(string cachekey, object val, TimeSpan sildingExpiration)
        {
            if (IsCache())
            _objCache.Add(cachekey, val, null, System.Web.Caching.Cache.NoAbsoluteExpiration, sildingExpiration, CacheItemPriority.High, null);
        }
        /// <summary>
        /// 设置缓存
        /// </summary>
        /// <param name="cachekey"></param>
        /// <param name="val"></param>
        /// <param name="absoluteExpiration"></param>
        /// <param name="onRemovedCallback"></param>
        public static void Set(string cachekey, object val, DateTime absoluteExpiration, System.Web.Caching.CacheItemRemovedCallback onRemovedCallback)
        {
           
            Set(cachekey, val, null, absoluteExpiration, TimeSpan.Zero, CacheItemPriority.High, onRemovedCallback);
        }
        /// <summary>
        /// 设置缓存
        /// </summary>
        /// <param name="cachekey"></param>
        /// <param name="val"></param>
        /// <param name="absoluteExpiration"></param>
        /// <param name="sildingExpiration"></param>
        /// <param name="onRemovedCallback"></param>
        public static void Set(string cachekey, object val, DateTime absoluteExpiration, TimeSpan sildingExpiration, System.Web.Caching.CacheItemRemovedCallback onRemovedCallback)
        {
            Set(cachekey, val, null, absoluteExpiration, sildingExpiration, CacheItemPriority.High, onRemovedCallback);
        }
        /// <summary>
        /// 设置缓存
        /// </summary>
        /// <param name="cachekey"></param>
        /// <param name="val"></param>
        /// <param name="dependencies"></param>
        /// <param name="absoluteExpiration"></param>
        /// <param name="sildingExpiration"></param>
        /// <param name="priority"></param>
        /// <param name="onRemovedCallback"></param>
        public static void Set(string cachekey, object val, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan sildingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemovedCallback)
        {
            CacheItemRemovedCallback callback = null;
            if (onRemovedCallback != null)
            {
                callback = onRemovedCallback;
            } 
            if (IsCache())
            _objCache.Add(cachekey, val, dependencies, absoluteExpiration, sildingExpiration, priority, callback);
        }
    }
}
